<?php

class Factura {

	private static $_instance;
	private $facturas = null;
	private $factura = null;
	private $conceito = null;
	private $estado = null;
	private $cidade = null;
	private $pais = null;
	private $paisB = null;

	public static function getInstance() {
		if (!(self::$_instance instanceof self)) {
			self::$_instance = new self();
		}
		return self::$_instance;
	}

	// Do not allow an explicit call of the constructor: $v = new Singleton();
	final private function __construct() {

	}

	// Do not allow the clone operation: $x = clone $v;
	final private function __clone() {

	}

	public function getFacturas() {

		if (!is_null($this->facturas)) {
			return $this->facturas;
		}
		global $user, $db;
		$empresa = $user->getempresa();
		$sql = '
	SELECT
  	FraID,
  	FraAlbaran,
  	FraOrigen0,
  	FraOrigen1,
  	OriNombre,
  	FraReferenciaAg,
  	FraFechaIngreso,
  	FraFechaAlb,
  	FraFecha,
  	FraCargoUSD,
  	FraEnviadoUSD,
  	FraEnviado,
  	FraCliNumero,
  	FraBnfNumero,
  	FraReferenciaCo,
  	FraCtaBenef,
  	FraCorresponsal,
  	FraARecibir,
  	FraCambio,
  	FraTasa,
  	FraTasaAgn,
  	FraCargo,
  	FraMonCobro,
  	FraMonDPago,
  	FraMonPagoISO,
  	FraStatus,
  	FraMensaje,
  	FraNotas,
  	FraConcepto,
  	FraModo,
  	FraCliNumero       AS CliNumero,
  	FraCliNombre       AS CliNombre,
  	FraCliDomicilio    AS CliDomicilio,
  	FraCliPortal       AS CliPortal,
  	FraCliPisoPuerta   AS CliPisoPuerta,
  	FraCliLocation     AS CliLocation,
  	FraCliCP           AS CliCP,
  	FraCliDNI          AS CliDNI,
  	FraCliNacionalidad AS CliNacionalidad,
  	FraCliTelefono     AS CliTelefono,
  	FraCliTelefono2    AS CliTelefono2,
  	FraCliCiudad       AS CliCiudad,
  	FraCliObs          AS ClObs,
  	FraBnfNombre       AS BnfNombre,
  	FraBnfNumero       AS BnfNumero,
  	FraBnfDireccion    AS BnfDireccion,
  	FraBnfCedula       AS BnfCedula,
  	FraBnfCiudad       AS BnfCiudad,
  	BnfCiu.Prov_ID     AS BnfProvincia,
  	FraBnfTelefono     AS BnfTelefono,
  	FraBnfPais         AS BnfPais,
  	FraBnfBarrio       AS BnfBarrio,
  	FraBnfDocType      AS BnfDocType,
  	FraCtaBenef        AS cb_Id,
  	FraCuenta          AS cb_Cuenta,
  	FraCuenta_prx      AS cb_Cuenta_prx,
  	FraCuenta_sux      AS cb_Cuenta_sux,
  	FraCuenta_mod      AS cb_Cuenta_mod,
  	FraSucursal        AS cb_Sucursal,
  	FraCiudadSucursal  AS cb_CiudadSucursal,
  	FraTipoCuenta      AS cb_TipoCuenta,
  	FraBancoID         AS cb_Banco,
  	miso_ID,
  	mp_ModoPago,
  	CorrNombre,
  	CorrNumero,
  	CorrDireccion,
  	pai_MonedaBDE,
  	main_users.name    AS FraOrigen2,
  	CliCiu.CiuPais     AS ClPais,
  	CliCiu.Prov_ID     AS CliProvincia
	FROM officeschema.facturas
  	INNER JOIN officeschema.facturasextjoins ON FraID = FraExtID
  	LEFT OUTER JOIN officeschema.corresponsales ON CorrNumero = FraCashierCorr
  	INNER JOIN officeschema.hd_modospago ON mp_ID = FraModoPago
  	LEFT OUTER JOIN officeschema.hd_monedasiso ON miso_Cod_elite = FraMonDPago
  	LEFT OUTER JOIN officeschema.hd_paises ON FraBnfPais = pai_CodElite
  	LEFT OUTER JOIN systemoffice.main_users ON main_users.ref = FraOrigen2
  	LEFT OUTER JOIN officeschema.origenes ON OriNumero = FraOrigen0
  	LEFT OUTER JOIN officeschema.ciudades AS CliCiu ON CliCiu.CiuNumero = FraCliCiudad
  	LEFT OUTER JOIN officeschema.ciudades AS BnfCiu ON BnfCiu.CiuNumero = FraBnfCiudad
    WHERE officeschema.facturas.FraCorresponsal=' . $empresa;
		$append = '';
		$pesquisas = 'FraID,FraStatus,FraEnviado,FraARecibir,CliNombre,BnfNombre,mp_ModoPago';
		$ordenacoes = $pesquisas;
		$coluna_a_ordenar_por_defeito = 'FraID';
		$ordem_a_ordenar_por_defeito = 'desc';
		$result = $db->getall($sql);
		$this->facturas = $result;
		return $this->facturas;
	}

	public function getFactura($numeroDaFactura) {

		global $user, $db;
		$empresa = $user->getempresa();

		$sql = '
	SELECT
  	FraID,
  	FraAlbaran,
  	FraOrigen0,
  	FraOrigen1,
  	OriNombre,
  	FraReferenciaAg,
  	FraFechaIngreso,
  	FraFechaAlb,
  	FraFecha,
  	FraCargoUSD,
  	FraEnviadoUSD,
  	FraEnviado,
  	FraCliNumero,
  	FraBnfNumero,
  	FraReferenciaCo,
  	FraCtaBenef,
  	FraCorresponsal,
  	FraARecibir,
  	FraCambio,
  	FraTasa,
  	FraTasaAgn,
  	FraCargo,
  	FraMonCobro,
  	FraMonDPago,
  	FraMonPagoISO,
  	FraStatus,
  	FraMensaje,
  	FraNotas,
  	FraConcepto,
  	FraModo,
  	FraCliNumero       AS CliNumero,
  	FraCliNombre       AS CliNombre,
  	FraCliDomicilio    AS CliDomicilio,
  	FraCliPortal       AS CliPortal,
  	FraCliPisoPuerta   AS CliPisoPuerta,
  	FraCliLocation     AS CliLocation,
  	FraCliCP           AS CliCP,
  	FraCliDNI          AS CliDNI,
  	FraCliNacionalidad AS CliNacionalidad,
  	FraCliTelefono     AS CliTelefono,
  	FraCliTelefono2    AS CliTelefono2,
  	FraCliCiudad       AS CliCiudad,
  	FraCliObs          AS ClObs,
  	FraBnfNombre       AS BnfNombre,
  	FraBnfNumero       AS BnfNumero,
  	FraBnfDireccion    AS BnfDireccion,
  	FraBnfCedula       AS BnfCedula,
  	FraBnfCiudad       AS BnfCiudad,
  	BnfCiu.Prov_ID     AS BnfProvincia,
  	FraBnfTelefono     AS BnfTelefono,
  	FraBnfPais         AS BnfPais,
  	FraBnfBarrio       AS BnfBarrio,
  	FraBnfDocType      AS BnfDocType,
  	FraCtaBenef        AS cb_Id,
  	FraCuenta          AS cb_Cuenta,
  	FraCuenta_prx      AS cb_Cuenta_prx,
  	FraCuenta_sux      AS cb_Cuenta_sux,
  	FraCuenta_mod      AS cb_Cuenta_mod,
  	FraSucursal        AS cb_Sucursal,
  	FraCiudadSucursal  AS cb_CiudadSucursal,
  	FraTipoCuenta      AS cb_TipoCuenta,
  	FraBancoID         AS cb_Banco,
  	miso_ID,
  	mp_ModoPago,
  	CorrNombre,
  	CorrNumero,
  	CorrDireccion,
  	pai_MonedaBDE,
  	main_users.name    AS FraOrigen2,
  	CliCiu.CiuPais     AS ClPais,
  	CliCiu.Prov_ID     AS CliProvincia
	FROM officeschema.facturas
  	INNER JOIN officeschema.facturasextjoins ON FraID = FraExtID
  	LEFT OUTER JOIN officeschema.corresponsales ON CorrNumero = FraCashierCorr
  	INNER JOIN officeschema.hd_modospago ON mp_ID = FraModoPago
  	LEFT OUTER JOIN officeschema.hd_monedasiso ON miso_Cod_elite = FraMonDPago
  	LEFT OUTER JOIN officeschema.hd_paises ON FraBnfPais = pai_CodElite
  	LEFT OUTER JOIN systemoffice.main_users ON main_users.ref = FraOrigen2
  	LEFT OUTER JOIN officeschema.origenes ON OriNumero = FraOrigen0
  	LEFT OUTER JOIN officeschema.ciudades AS CliCiu ON CliCiu.CiuNumero = FraCliCiudad
  	LEFT OUTER JOIN officeschema.ciudades AS BnfCiu ON BnfCiu.CiuNumero = FraBnfCiudad
    WHERE officeschema.facturas.FraCorresponsal=' . $empresa . ' AND officeschema.facturas.FraID=' . $numeroDaFactura;
		$result = $db->getall($sql);
		$this->factura = $result;

		$conceito = $result[0]->FraConcepto;
		$sql = 'SELECT tcn_name FROM officeschema.hd_transactionconcept WHERE tcn_ID=' . $conceito;
		$conceito = $db->getall($sql);
		$conceito = $conceito[0]->tcn_name;
		$this->conceito = $conceito;

		$estado = $result[0]->FraStatus;
		$sql = 'SELECT sg_Descripcion FROM officeschema.hd_statusfacturas WHERE sg_Id=' . $estado;
		$estado = $db->getall($sql);
		$estado = $estado[0]->sg_Descripcion;
		$this->estado = $estado;

		$cidade = $result[0]->BnfCiudad;
		$sql = 'SELECT  officeschema.ciudades.CiuCiudad FROM officeschema.ciudades WHERE officeschema.ciudades.CiuNumero=' . $cidade;
		$cidade = $db->getall($sql);
		$cidade = $cidade[0]->CiuCiudad;
		$this->cidade = $cidade;

		$pais = $result[0]->CliNacionalidad;
		$sql = 'SELECT officeschema.hd_paises.pai_NombreESP FROM officeschema.hd_paises WHERE officeschema.hd_paises.pai_CodElite =' . $pais;
		$pais = $db->getall($sql);
		$pais = $pais[0]->pai_NombreESP;
		$this->pais = $pais;

		$paisB = $result[0]->BnfPais;
		$sql = 'SELECT officeschema.hd_paises.pai_NombreESP FROM officeschema.hd_paises WHERE officeschema.hd_paises.pai_CodElite =' . $paisB;
		$paisB = $db->getall($sql);
		$paisB = $paisB[0]->pai_NombreESP;
		$this->paisB = $paisB;


		return $this->factura;
	}

	public function getConceito() {
		return $this->conceito;
	}

	public function getEstado() {
		return $this->estado;
	}

	public function getCidade() {
		return $this->cidade;
	}

	public function getPais() {
		return $this->pais;
	}

	public function getPaisB() {
		return $this->paisB;
	}

}